package com.hd.crdao.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.hd.crdao.R;
import com.hd.crdao.adapter.ProvinceAdapter;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.bean.AreaListBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.wrapper.CustomCallback;
import com.zhy.http.okhttp.OkHttpUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 *@author Administrator
 *@date 2017/5/26 0026
 */
public class AddressActivity extends BaseActivity {
    private static final int REQCODE = 1;  //请求码常量
    private static final int SENDMESSAGE = 666;
    @BindView(R.id.address_head)
    ExpandHeadView mAddressHead;
    @BindView(R.id.lv_address)
    ListView mLvAddress;
    public AreaListBean mAreaListBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);

    }

    @Override
    protected void initListener() {
        mAddressHead.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mLvAddress.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AreaListBean.ListAreaBean listAreaBean = mAreaListBean.getListArea().get(position);
                String parentId = listAreaBean.getId();
                String provinceName = listAreaBean.getName();
                String fromWhere = getIntent().getStringExtra("fromWhere");  //从意图中获取上个界面里面的falg
                Intent intent = new Intent(UIUtils.getContext(),CityActivity.class);
                intent.putExtra("parentId",parentId);
                intent.putExtra("provinceName",provinceName);
                intent.putExtra("fromWhere",fromWhere);
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    protected void initData() {

        try {
            OkHttpUtils.post().url(Constants.CITYURL)
                    .addParams("type","1")
                    .addParams("sign", Constants.SIGN).build()
                    .execute(new CustomCallback<AreaListBean>() {

                        @Override
                        public void onResponse(AreaListBean areaListBean, int id) {
                            mAreaListBean= areaListBean;
                    mLvAddress.setAdapter(new ProvinceAdapter(mAreaListBean.getListArea()));
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
